#include "Player.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <algorithm> // 用于min和max函数

using namespace std;

// 角色类方法实现
string Character::getName() const {
    return this->name;
}

int Character::getLevel() const {
    return this->level;
}

int Character::getMaxHealth() const {
    return this->maxHealth;
}

int Character::getCurrentHealth() const {
    return this->currentHealth;
}

int Character::getMaxMana() const {
    return this->maxMana;
}

int Character::getCurrentMana() const {
    return this->currentMana;
}

int Character::getAgility() const {
    return this->agility;
}

int Character::getStrength() const {
    return this->strength;
}

int Character::getLuck() const {
    return this->luck;
}

int Character::getWisdom() const {
    return this->wisdom;
}

int Character::getAccuracy() const {
    return this->accuracy;
}

int Character::getDodging() const {
    return this->dodging;
}

int Character::getDefense() const {
    return this->defense;
}

void Character::showInfo() const {
    cout << getName() << ":" << endl;
    cout << left
        << "等级：" << setw(15) << getLevel()
        << "生命：" << setw(15) << getCurrentHealth()
        << "法力：" << setw(15) << getCurrentMana() << endl
        << "敏捷：" << setw(15) << getAgility()
        << "力量：" << setw(15) << getStrength()
        << "准确度：" << setw(13) << getAccuracy() << endl
        << "躲避：" << setw(15) << getDodging()
        << "幸运：" << setw(15) << getLuck()
        << "智力：" << setw(15) << getWisdom()
        << "防御：" << setw(15) << getDefense() << endl;
    TextUtils::pressAnyKeyToContinue();
}

vector<Skill>& Character::getSkills() {
    return skills;
}

bool Character::isAlive() const {
    return this->currentHealth > 0;
}

BackPack* Character::getBackpack() {
    return backpack;
}

void Character::setBackpack(BackPack* backpack) {
    this->backpack = backpack;
}

void Character::takeDamage(int damage) {
    if (damage < 0) {
        damage = 0;
    }
    this->currentHealth -= damage;
    if (this->currentHealth < 0) {
        currentHealth = 0;
    }
    cout << this->name << "受到了" << damage << "点伤害！" << endl;
}

void Character::heal(int amount) {
    this->currentHealth += amount;
    if (this->currentHealth > this->maxHealth) {
        this->currentHealth = this->maxHealth;
    }
    cout << this->name << "恢复了" << amount << "点生命值！" << endl;
}

void Character::restoreMana(int amount) {
    this->currentMana += amount;
    if (this->currentMana > this->maxMana) {
        this->currentMana = this->maxMana;
    }
    if (amount > 0) {
        cout << this->name << "恢复了" << amount << "点法力值！" << endl;
    }
    else {
        cout << this->name << "消耗了" << abs(amount) << "点法力值！" << endl;
    }
}

void Character::addStatusEffect(StatusEffect effect) {
    // 检查是否已有相同类型的效果
    for (auto& se : this->statusEffects) {
        if (se.type == effect.type) {
            // 刷新持续时间
            se.duration = max(se.duration, effect.duration);
            // 增强效果
            se.potency = max(se.potency, effect.potency);
            return;
        }
    }
    statusEffects.push_back(effect);
    describeStatusEffect(effect, true);
}

void Character::describeStatusEffect(StatusEffect effect, bool isAdded) {
    string action = isAdded ? "陷入了" : "摆脱了";
    switch (effect.type) {
    case StatusEffect::POISONED:
        cout << this->name << action << "中毒状态！" << endl;
        break;
    case StatusEffect::BLEEDING:
        cout << this->name << action << "流血状态！" << endl;
        break;
    case StatusEffect::STUNNED:
        cout << this->name << action << "眩晕状态！" << endl;
        break;
    case StatusEffect::STRENGTH_BOOST:
        cout << this->name << (isAdded ? "获得了" : "失去了") << "力量提升效果！" << endl;
        break;
    case StatusEffect::DEFENSE_BOOST:
        cout << this->name << (isAdded ? "获得了" : "失去了") << "防御提升效果！" << endl;
        break;
    case StatusEffect::SAN_LOSS:
        cout << this->name << (isAdded ? "开始快速流失理智！" : "停止了理智流失！") << endl;
        break;
    case StatusEffect::SAN_LOCK:
        cout << this->name << (isAdded ? "的理智被暂时锁定！" : "的理智锁定效果消失了！") << endl;
        break;
    default:
        break;
    }
}

bool Character::hasStatusEffect(StatusEffect::Type type) const {
    for (const auto& se : this->statusEffects) {
        if (se.type == type) return true;
    }
    return false;
}

bool Character::processStatusEffects() {
    vector<StatusEffect> newEffects;
    for (auto& effect : this->statusEffects) {
        // 应用效果
        switch (effect.type) {
        case StatusEffect::POISONED:
            cout << this->name << "中了毒，";
            takeDamage(effect.potency);
            break;
        case StatusEffect::BLEEDING:
            cout << this->name << "在流血，";
            takeDamage(effect.potency);
            break;
        case StatusEffect::STUNNED:
            cout << this->name << "处于眩晕中，无法行动！" << endl;
            break;
        case StatusEffect::SAN_LOSS:
            // 理智流失效果（仅对玩家有效）
            if (dynamic_cast<Player*>(this)) {
                Player* player = static_cast<Player*>(this);
                cout << this->name << "的理智正在快速流失！";
                player->takeSanDamage(effect.potency);
            }
            break;
        default:
            break;
        }

        // 减少持续时间
        effect.duration--;

        // 如果还有持续时间，保留效果
        if (effect.duration > 0) {
            newEffects.push_back(effect);
        }
        else {
            describeStatusEffect(effect, false);
        }
    }

    statusEffects = newEffects;

    // 检查是否还活着
    return isAlive();
}

int Character::getEffectiveStrength() const {
    int eff = strength;
    for (const auto& se : statusEffects) {
        if (se.type == StatusEffect::STRENGTH_BOOST) {
            eff += se.potency;
        }
    }
    return eff;
}

int Character::getEffectiveDefense() const {
    int eff = this->defense;
    for (const auto& se : this->statusEffects) {
        if (se.type == StatusEffect::DEFENSE_BOOST) {
            eff += se.potency;
        }
    }
    return eff;
}

void Character::displayStatus() const {
    cout << this->name << ":" << endl
        << left
        << "等级：" << setw(15) << getLevel()
        << "生命：" << getMaxHealth() << '/' << setw(15) << getCurrentHealth()
        << "法力：" << getMaxMana() << '/' << setw(15) << getCurrentMana() << endl
        << "敏捷：" << setw(15) << getAgility()
        << "力量：" << setw(19) << getStrength()
        << "防御：" << setw(15) << getDefense() << endl;

    if (!statusEffects.empty()) {
        cout << "状态效果: ";
        for (const auto& se : this->statusEffects) {
            switch (se.type) {
            case StatusEffect::POISONED: cout << "中毒(" << se.duration << ") "; break;
            case StatusEffect::BLEEDING: cout << "流血(" << se.duration << ") "; break;
            case StatusEffect::STUNNED: cout << "眩晕(" << se.duration << ") "; break;
            case StatusEffect::STRENGTH_BOOST: cout << "力量提升(" << se.duration << ") "; break;
            case StatusEffect::DEFENSE_BOOST: cout << "防御提升(" << se.duration << ") "; break;
            case StatusEffect::SAN_LOSS: cout << "理智流失(" << se.duration << ") "; break;
            case StatusEffect::SAN_LOCK: cout << "理智锁定(" << se.duration << ") "; break;
            default: break;
            }
        }
        cout << endl;
    }
}

void Character::attack(Character* target) {
    // 计算命中率 (基于敏捷)
    int hitChance = 60 + (agility - target->agility) * 2;
    hitChance = max(20, min(90, hitChance)); // 命中率限制在20%-90%之间

    if (rand() % 100 < hitChance) {
        // 命中，计算伤害
        int damage = (strength * 2 - target->getEffectiveDefense()) / 2;
        damage = max(1, damage); // 至少造成1点伤害

        target->takeDamage(damage);

        // 有几率造成流血效果
        if (rand() % 100 < 10) { // 10%几率
            target->addStatusEffect(StatusEffect(StatusEffect::BLEEDING, 2, 3));
        }
    }
    else {
        cout << name << "的攻击没有命中！" << endl;
    }
}

void Character::chooseAction(Character* target) {
    // 空实现，由子类重写
}

// 玩家类方法实现
void Player::distribute() {
    this->name = "Charles";
    int total = 20;
    int i = 0;
    int health = 0, agi = 0, str = 0, acc = 0, dod = 0, luck = 0, wis = 0, defense = 0, mana = 0;
    cout << "请为Charles分配点数：（初始点数为" << total << ")" << endl;
    cout << "1.生命 2.敏捷 3.力量 4.准确度 5.躲避 6.幸运 7.智力 8.防御 9.法力" << endl;
    while (true) {
        cout << "请选择您要分配的属性：";
        int temp = 0;
        cin >> i;
        switch (i) {
        case 1:
            cout << "生命（生命值为点数*10）(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配" << endl;
            }
            else if (health + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                health += temp * 10;
                cout << "生命分配成功，生命值为" << health << endl;
            }
            break;
        case 2:
            cout << "敏捷(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配";
            }
            else if (agi + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                agi += temp;
                cout << "敏捷分配成功，敏捷点数为" << agi << endl;
            }
            break;
        case 3:
            cout << "力量(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配";
            }
            else if (str + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                str += temp;
                cout << "力量分配成功，力量点数为" << str << endl;
            }
            break;
        case 4:
            cout << "准确度(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配";
            }
            else if (acc + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                acc += temp;
                cout << "准确度分配成功，准确度点数为" << acc << endl;
            }
            break;
        case 5:
            cout << "躲避(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配";
            }
            else if (dod + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                dod += temp;
                cout << "躲避分配成功，躲避点数为" << dod << endl;
            }
            break;
        case 6:
            cout << "幸运(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配";
            }
            else if (luck + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                luck += temp;
                cout << "幸运分配成功，幸运点数为" << luck << endl;
            }
            break;
        case 7:
            cout << "智力(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配";
            }
            else if (wis + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                wis += temp;
                cout << "智力分配成功，智力点数为" << wis << endl;
            }
            break;
        case 8:
            cout << "防御(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配";
            }
            else if (defense + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                defense += temp;
                cout << "防御分配成功，防御点数为" << defense << endl;
            }
            break;
        case 9:
            cout << "法力（法力值为点数*5）(可用点数：" << total << ")：";
            cin >> temp;
            if (temp > total) {
                cout << "超出初始值，请重新分配";
            }
            else if (mana + temp < 0) {
                cout << "属性不可为负值，请重新分配" << endl;
            }
            else {
                total -= temp;
                mana += temp * 5;
                cout << "法力分配成功，法力值为" << mana << endl;
            }
            break;
        }
        if (total == 0) {
            break;
        }
    }
    this->maxHealth = health;
    this->currentHealth = this->maxHealth;
    this->maxMana = mana;
    this->currentMana = this->maxMana;
    this->agility = agi;
    this->strength = str;
    this->accuracy = acc;
    this->dodging = dod;
    this->luck = luck;
    this->wisdom = wis;
    this->defense = defense;
}

// 获取物品列表
const std::vector<Item>& Player::getInventory() const {
    static const std::vector<Item> emptyInventory; // 静态空列表，避免返回空指针

    // 检查背包是否存在
    if (backpack) {
        return emptyInventory;
    }

    // 如果背包不存在，返回空列表
    return emptyInventory;
}

// 检查物品列表是否为空
bool Player::isInventoryEmpty() const {
    return true;
}

// 获取物品总数
size_t Player::getInventorySize() const {
    return 0;
}

void Player::buff() {
    this->maxHealth += 50;
    this->currentHealth += 50;
    this->maxMana += 25;
    this->currentMana += 25;
    this->agility += 5;
    this->strength += 5;
    this->accuracy += 5;
    this->dodging += 5;
    this->luck += 5;
    this->wisdom += 5;
    this->sanSpeed *= 0.8;
    this->defense += 5;
    cout << "Charles得到教皇的祝福！所有基础属性点数+5，san值掉落速率*0.8！当前生命值+50，当前法力值+25！" << endl;
}

void Player::withoutBuff() {
    this->maxHealth -= 50;
    if (this->currentHealth > this->maxHealth) {
        this->currentHealth = this->maxHealth;
    }
    this->maxMana -= 25;
    if (this->currentMana > this->maxMana) {
        this->currentMana = this->maxMana;
    }
    this->agility -= 5;
    this->strength -= 5;
    this->accuracy -= 5;
    this->dodging -= 5;
    this->luck -= 5;
    this->wisdom -= 5;
    this->sanSpeed *= 1.25;
    this->defense -= 5;
    cout << "Charles失去教皇的祝福！" << endl;
}

void Player::showBag() {
    // 背包展示实现（根据BackPack类具体实现）
    if (backpack) {
        backpack->displayItems();
    }
    else {
        cout << "你的背包是空的。" << endl;
    }
}

void Player::showInfo() const {
    cout << getName() << ":" << endl;
    cout << left
        << "等级：" << setw(15) << getLevel()
        << "生命：" << getCurrentHealth() << "/" << setw(10) << getMaxHealth()
        << "法力：" << getCurrentMana() << "/" << setw(10) << getMaxMana() << endl
        << "经验：" << setw(15) << getExpoints()
        << "敏捷：" << setw(15) << getAgility()
        << "力量：" << setw(15) << getStrength() << endl
        << "准确度：" << setw(13) << getAccuracy()
        << "躲避：" << setw(15) << getDodging()
        << "幸运：" << setw(15) << getLuck() << endl
        << "智力：" << setw(15) << getWisdom()
        << "金钱：" << setw(15) << getMoney()
        << "san值：" << getSan() << "/" << setw(8) << getMaxSan() << endl
        << "防御：" << setw(15) << getDefense()
        << "重生次数：" << setw(11) << getReamount()
        << "san值掉落速率：" << fixed << setprecision(2) << getSanSpeed() << endl;
}

void Player::setSkills(string name, string desc, int dmg, int mana, bool offensive, StatusEffect::Type type, int probability, int duration) {
    this->getSkills().push_back(Skill(name, desc, dmg, mana, offensive, type, probability, duration));
}

void Player::setMaxHealth(int health) {
    this->maxHealth += health * 10;
    this->currentHealth += health * 10;
}

void Player::setMaxMana(int mana) {
    this->maxMana += mana * 5;
    this->currentMana += mana * 5;
}

void Player::setDefense(int defense) {
    this->defense += defense;
}

void Player::setAgility(int agi) {
    this->agility += agi;
}

void Player::setLuck(int luck) {
    this->luck += luck;
}

void Player::setStrength(int str) {
    this->strength += str;
}

void Player::setWisdom(int wis) {
    this->wisdom += wis;
}

void Player::setAccuracy(int acc) {
    this->accuracy += acc;
}

void Player::setDodging(int dod) {
    this->dodging += dod;
}

int Player::getExpoints() const {
    return this->expoints;
}

void Player::setExpoints(int e) {
    int currentLevel = getLevel();
    int currentExp = this->expoints + e;
    if (currentLevel >= 50) {
        cout << "已经达到满级，无法再获得经验！" << endl;
        return;
    }

    cout << "你获得了" << e << "点经验值！" << endl;

    int RequiredExp = 0;
    int temp = currentLevel / 10;
    switch (temp) {
    case 4: RequiredExp = 50; break;
    case 3: RequiredExp = 40; break;
    case 2: RequiredExp = 30; break;
    case 1: RequiredExp = 20; break;
    case 0: RequiredExp = 10; break;
    }

    while (currentLevel < 50 && currentExp >= RequiredExp) {
        currentExp -= RequiredExp;
        this->level += 1;
        this->maxHealth += 10;
        this->currentHealth += 10;
        this->maxMana += 5;
        this->currentMana += 5;
        this->agility += 1;
        this->strength += 1;
        this->accuracy += 1;
        this->dodging += 1;
        this->luck += 1;
        this->wisdom += 1;
        this->defense += 1;
        this->sanSpeed *= 0.95;
        this->increaseMaxSan(2); // 升级时增加最大理智值
        this->expoints = currentExp;

        cout << "恭喜升级！当前等级: " << getLevel()
            << ", 剩余经验: " << this->expoints << endl;

        currentLevel = getLevel();
        temp = currentLevel / 10;
        switch (temp) {
        case 4: RequiredExp = 50; break;
        case 3: RequiredExp = 40; break;
        case 2: RequiredExp = 30; break;
        case 1: RequiredExp = 20; break;
        case 0: RequiredExp = 10; break;
        }

        if (currentLevel >= 50) {
            currentExp = 0;
            this->expoints = 0;
            cout << "恭喜达到满级50级！" << endl;
            break;
        }
    }

    this->expoints = currentExp;
}

int Player::getMoney() const {
    return this->money;
}

void Player::PayMoney(int money) {
    if (this->money < money) {
        cout << "金钱不足" << endl;
    }
    else {
        this->money -= money;
        cout << "花费了" << money << "金币，剩余" << this->money << "金币" << endl;
    }
}

void Player::EarnMoney(int money) {
    this->money += money;
}

int Player::getReamount() const {
    return this->reamount;
}

void Player::setReamount() {
    this->reamount++;
}

// 理智系统相关方法实现
double Player::getSan() const {
    return this->san;
}

double Player::getMaxSan() const {
    return this->maxSan;
}

void Player::restoreSan(double amount) {
    if (amount <= 0) {
        return; // 只处理正值恢复
    }

    // 检查是否有理智锁定状态
    if (hasStatusEffect(StatusEffect::SAN_LOCK)) {
        cout << "理智被锁定，无法恢复！" << endl;
        return;
    }

    this->san += amount;
    if (this->san > this->maxSan) {
        this->san = this->maxSan;
    }
    cout << "恢复了" << fixed << setprecision(1) << amount << "点理智值！当前理智: "
        << fixed << setprecision(1) << this->san << "/" << this->maxSan << endl;
}

void Player::takeSanDamage(double amount) {
    if (amount <= 0) {
        return; // 只处理正值伤害
    }

    // 检查是否有理智锁定状态
    if (hasStatusEffect(StatusEffect::SAN_LOCK)) {
        cout << "理智被锁定，免受理智伤害！" << endl;
        return;
    }

    this->san -= amount;
    if (this->san < 0) {
        this->san = 0;
    }
    cout << "失去了" << fixed << setprecision(1) << amount << "点理智值！当前理智: "
        << fixed << setprecision(1) << this->san << "/" << this->maxSan << endl;
}

void Player::setSan(double san) {
    // 直接设置理智值，带边界检查
    if (hasStatusEffect(StatusEffect::SAN_LOCK)) {
        cout << "理智被锁定，无法直接修改！" << endl;
        return;
    }

    this->san = san;
    if (this->san < 0) {
        this->san = 0;
    }
    else if (this->san > this->maxSan) {
        this->san = this->maxSan;
    }
}

double Player::getSanSpeed() const {
    return this->sanSpeed;
}

void Player::setSanSpeed(double sp) {
    this->sanSpeed *= sp;
    // 限制速率范围，防止极端值
    if (this->sanSpeed < 0.1) {
        this->sanSpeed = 0.1;
    }
    else if (this->sanSpeed > 10.0) {
        this->sanSpeed = 10.0;
    }
}

void Player::increaseMaxSan(double amount) {
    if (amount <= 0) return;

    this->maxSan += amount;
    cout << "最大理智值提升了" << fixed << setprecision(1) << amount
        << "点，当前最大理智: " << this->maxSan << endl;
}

void Player::chooseAction(Character* target) {
    // 检查是否眩晕
    if (hasStatusEffect(StatusEffect::STUNNED)) {
        cout << "你处于眩晕中，无法行动！" << endl;
        return;
    }

    cout << endl << "请选择行动:" << endl;
    cout << "1. 普通攻击" << endl;

    // 列出技能
    for (size_t i = 0; i < skills.size(); i++) {
        cout << i + 2 << ". 使用" << skills[i].getName() << " ("
            << skills[i].getManaCost() << " MP) - " << skills[i].getDescription() << endl;
    }

    int choice;
    cin >> choice;

    if (choice == 1) {
        attack(target);
    }
    else if (choice >= 2 && choice <= (int)skills.size() + 1) {
        skills[choice - 2].execute(this, target);
    }
    else {
        cout << "无效的选择，你犹豫了一下！" << endl;
    }
}

void Player::Respawn(int backupLife, int backupMana) {
    this->currentHealth = backupLife;
    this->currentMana = backupMana;
    // 复活时恢复部分理智
    this->restoreSan(this->maxSan * 0.3);
    setReamount();
    cout << "复活成功，复活次数+1" << endl;
}

// 怪物类方法实现
int Monster::getExpReward() const {
    return this->expReward;
}

void Monster::chooseAction(Character* target) {
    // 检查是否眩晕
    if (hasStatusEffect(StatusEffect::STUNNED)) {
        cout << name << "处于眩晕中，无法行动！" << endl;
        return;
    }

    // 低生命值时可能使用防御技能，否则随机攻击或使用技能
    if (currentHealth < maxHealth / 3 && !skills.empty() && rand() % 100 < 30) {
        // 30%几率使用防御性技能
        for (const auto& skill : skills) {
            if (!skill.isOffensiveSkill()) {
                Skill nonConstSkill = skill; // 创建临时副本以调用非const方法
                nonConstSkill.execute(this, target);
                return;
            }
        }
    }

    // 否则有70%几率普通攻击，30%几率使用攻击性技能
    if (!skills.empty() && rand() % 100 < 30) {
        // 选择一个攻击性技能
        vector<Skill> offensiveSkills;
        for (const auto& skill : skills) {
            if (skill.isOffensiveSkill() && currentMana >= skill.getManaCost()) {
                offensiveSkills.push_back(skill);
            }
        }

        if (!offensiveSkills.empty()) {
            int choice = rand() % offensiveSkills.size();
            Skill nonConstSkill = offensiveSkills[choice]; // 创建临时副本以调用非const方法
            nonConstSkill.execute(this, target);
            return;
        }
    }

    // 普通攻击
    attack(target);
}

Monster Monster::createRandomMonster(int level) {
    srand(time(0)); // 初始化随机数生成器
    // 怪物模板
    vector<Monster> monsterTemplates = {
        Monster("食尸鬼", 1, 40, 10, 6, 7, 2, 30),
        Monster("飞天水螅", 1, 50, 0, 8, 5, 4, 40),
        Monster("冷蛛", 1, 35, 0, 7, 9, 1, 35),
        Monster("克托尼亚人", 2, 60, 40, 5, 6, 3, 60),
        Monster("廷达罗斯之猎犬", 2, 80, 10, 12, 3, 6, 70)
    };

    // 根据玩家等级选择合适的怪物
    vector<Monster> possibleMonsters;
    for (const auto& m : monsterTemplates) {
        if (abs(m.getLevel() - level) <= 1) {
            possibleMonsters.push_back(m);
        }
    }

    // 如果没有合适的，就选第一个
    if (possibleMonsters.empty()) {
        possibleMonsters.push_back(monsterTemplates[0]);
    }

    // 随机选择一个怪物
    Monster monster = possibleMonsters[rand() % possibleMonsters.size()];

    // 给怪物添加一些技能
    if (monster.getName() == "食尸鬼") {
        monster.getSkills().push_back(Skill("噬咬", "对敌人进行噬咬", 12, 5, true, StatusEffect::BLEEDING, 30, 4));
    }
    else if (monster.getName() == "飞天水螅") {
        monster.getSkills().push_back(Skill("贪婪撕咬", "附在敌人身上，对敌人撕咬", 15, 5, true, StatusEffect::BLEEDING, 35, 4));
    }
    else if (monster.getName() == "冷蛛") {
        monster.getSkills().push_back(Skill("致命毒素", "对敌人注入致命的毒素", 16, 5, true, StatusEffect::POISONED, 40, 4));
    }
    else if (monster.getName() == "克托尼亚人") {
        monster.getSkills().push_back(Skill("狂怒", "提高自己的力量", 0, 5, false, StatusEffect::STRENGTH_BOOST, 100, 8));
    }
    else if (monster.getName() == "廷达罗斯之猎犬") {
        monster.getSkills().push_back(Skill("不死之身", "提高自己的防御", 0, 5, false, StatusEffect::DEFENSE_BOOST, 100, 8));
    }

    return monster;
}

// 技能类方法实现
string Skill::getName() const {
    return this->name;
}

string Skill::getDescription() const {
    return this->description;
}

int Skill::getDamage() const {
    return this->damage;
}

int Skill::getManaCost() const {
    return this->manaCost;
}

bool Skill::isOffensiveSkill() const {
    return this->isOffensive;
}

void Skill::execute(Character* caster, Character* target) {
    // 检查法力是否足够
    if (caster->getCurrentMana() < this->manaCost) {
        cout << caster->getName() << "法力不足，无法使用" << this->name << "！" << endl;
        return;
    }
    // 消耗法力
    caster->restoreMana(-this->manaCost);
    cout << caster->getName() << "使用了" << this->name << "！" << endl;

    if (isOffensive) {
        // 计算命中率 (基于敏捷)
        int hitChance = 70 + (caster->getAgility() - target->getAgility()) * 2;
        hitChance = max(30, min(95, hitChance)); // 命中率限制在30%-95%之间

        if (rand() % 100 < hitChance) {
            // 命中，计算伤害
            int damage = this->damage + (caster->getEffectiveStrength() - target->getEffectiveDefense() / 2) / 3;
            damage = max(1, damage); // 至少造成1点伤害
            target->takeDamage(damage);

            // 尝试施加状态效果
            if (effect != StatusEffect::NONE && rand() % 100 < effectChance) {
                target->addStatusEffect(StatusEffect(effect, 3, effectPotency));
            }
        }
        else {
            cout << caster->getName() << "的" << this->name << "没有命中！" << endl;
        }
    }
    else {
        // 防御性技能
        if (effect == StatusEffect::STRENGTH_BOOST) {
            caster->addStatusEffect(StatusEffect(this->effect, 5, this->effectPotency));
        }
        else if (effect == StatusEffect::DEFENSE_BOOST) {
            caster->addStatusEffect(StatusEffect(this->effect, 5, this->effectPotency));
        }
        else if (effect == StatusEffect::SAN_LOCK) {
            caster->addStatusEffect(StatusEffect(this->effect, 3, this->effectPotency));
        }
        else {
            // 治疗技能
            caster->heal(this->damage);
        }
    }
}
